package com.csj.mvvmtest.ui.third

import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.FragmentActivity
import com.csj.mvvm.ui.base.BaseAdapter2
import com.csj.mvvm.utils.ToastUtil
import com.csj.mvvmtest.R
import com.csj.mvvmtest.bean.ThirdBean
import com.csj.mvvmtest.databinding.ItemFlBinding
import com.csj.mvvmtest.databinding.ItemThirdBinding
import com.csj.mvvmtest.widget.FlowGroupView


class ThirdAdapter(context: FragmentActivity, listDatas: ArrayList<ThirdBean.DataBean>) :
    BaseAdapter2<ItemThirdBinding, ThirdBean.DataBean>(context, listDatas) {

    override fun getLayoutId(): Int {
        return R.layout.item_third
    }

    override fun convert(v: ItemThirdBinding, t: ThirdBean.DataBean, position: Int) {
        v.title.text = t.name
        v.fl.removeAllViews()
        for (s in t.children) {
            addTextView(v.fl, s)
        }
    }

    private fun addTextView(
        view: FlowGroupView,
        s: ThirdBean.DataBean.ChildrenBean
    ) {
        val child = ItemFlBinding.inflate(LayoutInflater.from(view.context), view, false)
        child.tv.text = s.name
        initEvents(child.root, s) //监听
        view.addView(child.root)
    }

    /**
     * 为每个view 添加点击事件
     */
    private fun initEvents(
        tv: View,
        s: ThirdBean.DataBean.ChildrenBean
    ) {
        tv.setOnClickListener {
            ToastUtil.showToast(s.name)
        }
    }

}